package sk.mucha.tracking;

import java.util.UUID;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.validation.EqualPasswordInputValidator;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.validation.IValidatable;
import org.apache.wicket.validation.IValidator;
import org.apache.wicket.validation.validator.AbstractValidator;
import org.apache.wicket.validation.validator.EmailAddressPatternValidator;
import org.apache.wicket.validation.validator.EmailAddressValidator;
import org.apache.wicket.validation.validator.StringValidator;

import com.visural.wicket.behavior.beautytips.BeautyTipBehavior;

import sk.mucha.tracking.classes.AjaxEmailValidator;
import sk.mucha.tracking.classes.AjaxFieldValidator;
import sk.mucha.tracking.classes.Captcha;
import sk.mucha.tracking.classes.PasswordPolicyValidator;

public class RegistrationPage extends PageTemplate {

	private FeedbackPanel feedback;
	
	public RegistrationPage() {
		
		feedback = new FeedbackPanel("feedback");
		feedback.setOutputMarkupId(true);
		
		TextField<String> emailField = new TextField<String>("email",Model.of(""));
		emailField.setLabel(new ResourceModel("label.email"));
		emailField.setRequired(true);
		emailField.add(EmailAddressValidator.getInstance());
		emailField.add(new AjaxEmailValidator());
		emailField.add(new AjaxFieldValidator(feedback));
		
		FormComponent<String> password1 = new PasswordTextField("password1", Model.of(""));
		password1.setLabel(Model.of("Password"));
		password1.setLabel(new ResourceModel("label.password"));
		password1.add(StringValidator.minimumLength(8));
		password1.add(new PasswordPolicyValidator());
		password1.add(new AjaxFieldValidator(feedback));
		
		FormComponent<String> password2 = new PasswordTextField("password2", Model.of(""));
		password2.setLabel(new ResourceModel("label.password2"));
		password2.add(new AjaxFieldValidator(feedback));
		
		TextField<?> name = new TextField<String>("name",Model.of(""));
		name.setRequired(true);
		name.setLabel(new ResourceModel("label.name"));
		name.add(new AjaxFieldValidator(feedback));
		
		TextField<?> surname = new TextField<String>("surname",Model.of(""));
		surname.setRequired(true);
		surname.setLabel(new ResourceModel("label.surname"));
		surname.add(new AjaxFieldValidator(feedback));
		
		Form< ? > form = new Form<Void>("registrationForm") {
			
			@Override
			protected void onSubmit() {
				info("Form successfully submitted");
			}
		};
		
		form.add(new EqualPasswordInputValidator(password1, password2));
		add(form);
		
		form.add(new Label("emailLabel", new ResourceModel("label.email")));
		form.add(new Label("nameLabel", new ResourceModel("label.name")));
		form.add(new Label("surnameLabel", new ResourceModel("label.surname")));
		form.add(new Label("passwordLabel", new ResourceModel("label.password")));
		form.add(new Label("password2Label", new ResourceModel("label.password2")));
		form.add(new Label("captchaLabel", new ResourceModel("label.captcha")));
		
		form.add(emailField.add(new BeautyTipBehavior(getString("enterEmail"))));
		form.add(password1.add(new BeautyTipBehavior(getString("enterPasswd"))));
		form.add(password2.add(new BeautyTipBehavior(getString("reenterPasswd"))));
		form.add(name.add(new BeautyTipBehavior(getString("enterName"))));
		form.add(surname.add(new BeautyTipBehavior(getString("enterSurname"))));
		form.add(new Captcha("captcha", feedback));
		
		add(feedback);
	}
	
	@Override
	public String getPageTitle() {
		return "Registration";
	}

}
